import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class TankClient extends Frame {

	public static final int GAME_WIDTH = 800;
	public static final int GAME_HEIGHT = 800;
	//private int x = 50,y = 50;
	private Image offScreenImage = null;
	
	private Tank myTank = new Tank(700, 700,true,Tank.Direction.STOP,this);
	private Wall w1 = new Wall(100, 200,20,150, this);
	private Wall w2 = new Wall(300, 300, 300, 20, this);
	Blood b = new Blood();
//	private Tank enomyTank = new Tank (80, 80,false,this);
	protected List<Tank> tanks = new ArrayList<Tank>();
	protected   List<Explode> explodes = new ArrayList<Explode>();
	protected List<Missile> missiles = new ArrayList<Missile>();
	
	private void launch() {
		
		for (int i = 0; i < 10; i++) {
			Tank tank = new Tank(50 + 40 * (i + 1), 80, false,Tank.Direction.DOWN,this);
			tanks.add(tank);
		}
		setTitle("TankWar");
		setSize(GAME_WIDTH, GAME_HEIGHT);
		setResizable(false);
		setLocation(200, 200);
		setBackground(Color.WHITE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				System.exit(0);
			}
		});
		addKeyListener(new KeyMonitor());
		
		setVisible(true);
		
		PaintThread paintThread = new PaintThread();
		Thread myThread = new Thread(paintThread);
		myThread.start();
		
	}

	
	@Override
	public void update(Graphics g) {
		if (offScreenImage == null) {
			offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
		}
		Graphics gOffScreenImage = offScreenImage.getGraphics();
		Color c = gOffScreenImage.getColor();
		gOffScreenImage.setColor(Color.WHITE);
		gOffScreenImage.fillRect(0, 0, GAME_WIDTH,GAME_HEIGHT);
		gOffScreenImage.setColor(c);
		paint(gOffScreenImage);
		g.drawImage(offScreenImage, 0, 0, null);

	}


	@Override
	public void paint(Graphics g) {
		g.drawString("missiles count " + missiles.size(), 10, 50);
		g.drawString("explodes count " + explodes.size(), 10, 70);
		g.drawString("tanks    count " + tanks.size(), 10, 90);
		g.drawString("tanks    life " + myTank.getLife(), 10, 110);
//tanks.draw(g);
		if (tanks.size()==0) {
			for (int i = 0; i < 5; i++) {
				Tank tank = new Tank(50 + 40 * (i + 1), 80, false,Tank.Direction.DOWN,this);
				tanks.add(tank);
			}
		}
		myTank.draw(g);	
		myTank.eat(b);
		w1.draw(g);
		w2.draw(g);
		b.draw(g);
		//explode.draw(g);
		for (int i = 0; i < tanks.size(); i++) {
			Tank t = tanks.get(i);
			t.collidesWithWall(w1); 
			t.collidesWithWall(w2); 
			t.colledesWithTanks(tanks);
			t.draw(g);
		}
		
		for (int i =0; i <missiles.size(); i++ )
		{
			Missile m = missiles.get(i);
			/*if (!m.isLive()) {
				missiles.remove(m);
			}else {
				m.draw(g);
			}*/
			m.hitTanks(tanks);
			m.hitTank(myTank);
			m.collidesWithWall(w1);
			m.collidesWithWall(w2);
			m.draw(g);
		}
		/*if (myMissile != null) {
			myMissile.draw(g);
		}*/
		//y+=5;
		
		for (int i =0; i <explodes.size(); i++ )
		{
			Explode e = explodes.get(i);
			e.draw(g);
			
		}

	}

	private class PaintThread implements Runnable {

		public void run() {
			while (true) {
				repaint();
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				 
			}
		}
	}

	private class KeyMonitor extends KeyAdapter {

		@Override
		public void keyPressed(KeyEvent g) {
			myTank.keyPressed(g);
		}

		@Override
		public void keyReleased(KeyEvent g) {
			myTank.keyReleased(g);
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new TankClient().launch();
	}

}
